from ship_envs.core.action import Action
from ship_envs.core.eitity import Entity
from ship_envs.core.range import Range
from ship_envs.core.rect_model import RectModel

import numpy as np


class Ship(Entity):

    def __init__(self):
        super(Ship, self).__init__()
        self._show_model = RectModel()
        self._show_model.color = np.array([1, 0.5, 0.5])
        self._show_model.angle = self.start_angle
        self._collision_model = RectModel(0, 0, length=0.08, width=0.04)
        self._collision_model.angle = self.start_angle

        self.range = Range()  # 设置船运动范围
        self.cur_speed = 0.0
        self.dt = 1

        self.cur_action = 0

    def is_agent(self):
        """
        标记船是否为需要训练的船
        :return:
        """
        return True

    def step(self, action):
        self.cur_action = action  # 记录动作
        self.move_by_action(action)


    def move_by_action(self, action):
        # 暂时按离散动作进行测试
        # if action == 0: 动作0 不做改变
        if action == Action.TURNLEFT.value:
            self._collision_model.rotate(1)
            self._show_model.rotate(1)
        elif action == Action.TURNRIGHT.value:
            self._collision_model.rotate(-1)
            self._show_model.rotate(-1)
        self._collision_model.move(self.cur_speed * self.dt)
        self._show_model.move(self.cur_speed * self.dt)


    def set_range(self, range):
        self.range = range

    # 是否超出限制范围
    def out_range(self):
        if self.range:
            return self.range.out_range(self)
        return False

    # 获取当前的中心点
    def get_pos(self):
        return self._collision_model.get_pos()

    def set_pos(self, pos):
        self._show_model.set_pos(pos)
        self._collision_model.set_pos(pos)

    def set_angle(self, angle):
        self._show_model.set_angle(angle)
        self._collision_model.set_angle(angle)





